package com.htjx.mall.admin.controllers;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.product.Catalog;
import com.htjx.mall.commons.models.product.Product;
import com.htjx.mall.commons.models.product.Specification;
import com.htjx.mall.services.CatalogService;
import com.htjx.mall.services.ProductService;
import com.htjx.mall.services.SpecificationService;

@Controller
@RequestMapping("/admin/catalog")
public class _CatalogController {
	@Autowired
	private ProductService productService;
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private SpecificationService specificationService; 
	
	
	@RequestMapping(value="/add/{product_id}", method = RequestMethod.GET)
	public String add(@PathVariable("product_id") long product_id, Model model) {
		Catalog catalog = new Catalog();
		catalog.setProduct_id(product_id);
		model.addAttribute("catalog", catalog);
		return "catalog/add";
	}
	
	@RequestMapping(value="/add", method = RequestMethod.POST)
	public String add(Catalog catalog, Model model) {
		String name = catalog.getName();
		if(StringUtils.isBlank(name)) {
			model.addAttribute("result", new Result(9,"请填写品类名称"));
			return "catalog/add";
		}
		
		catalog.setCreate_time(new Date());
		catalogService.insertOne(catalog);		
		return "redirect:/admin/product/detail/"+catalog.getProduct_id();
	}
	
	@RequestMapping(value = "/detail/{catalog_id}", method = RequestMethod.GET)
	public String catalogDetail(@PathVariable("catalog_id") long id, Model model) {
		Catalog catalog = catalogService.selectById(id);
		Result result = null;
		if(catalog == null) {
			result = new Result(9,"查询商品失败");
		} 
		
		Product product = productService.findUniqueBy("id", String.valueOf(catalog.getProduct_id()));
		
		// 查询该商品的品类
		List<Specification> listSpec = specificationService.SelectByCatalogId(id);
		 
		model.addAttribute("result", result);
		model.addAttribute("product", product);
		model.addAttribute("catalog", catalog);
		model.addAttribute("list_specification", listSpec);
		return "catalog/detail";
	}
	
	@RequestMapping(value="/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") long id, Model model) {
		Catalog catalog = catalogService.selectById(id);
		model.addAttribute("catalog", catalog);
		return "catalog/update";
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public String update(Catalog catalog, Model model) {
		Catalog ref = catalogService.selectById(catalog.getId());
		if(ref == null) {
			Result result = new Result(9, "数据有误");
			model.addAttribute("result", result);
			return "redirect:/admin/catalog/detail/"+catalog.getId();
		}
		
		String name = catalog.getName();
		if(StringUtils.isBlank(name)) {
			model.addAttribute("result", new Result(9,"请填写品类名称"));
			return "redirect:/admin/catalog/detail/"+catalog.getId();
		}
		catalogService.updateOne(catalog);
		return "redirect:/admin/catalog/detail/"+catalog.getId();
	}
	
	@RequestMapping(value="/delete/{product_id}/{id}")
	public String delete(@PathVariable("id") long id, @PathVariable("product_id") long product_id) {
		catalogService.deleteOne(id);
		return "redirect:/admin/product/detail/"+product_id;
	}
}
